<template>
	<view class="main_view">
		<view class="top_view">
			<!-- 头像 -->
			<img :src="urls" class="header_cls" @click="selectImg()"/>
			
			<view class="input_cls">
				账号: <input v-model="form.username" placeholder="请输入账号"/>
			</view>
			<view class="input_cls">
				密码: <input v-model="form.password" placeholder="请输入密码"/>
			</view>
			<view class="input_cls">
				昵称: <input v-model="form.nickname" placeholder="请输入昵称"/>
			</view>
			<view class="input_cls">
				邮箱: <input v-model="form.email" placeholder="请输入邮箱"/>
			</view>
			<button class="btn_cls" @click="register()">注册</button>
			<button class="btn_login_cls" @click="goLogin()">去登陆</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				urls: "/static/header.jpeg",
				form: {
					username: "",
					password: "",
					nickname: "",
					email: "",
					header: ""
				}
			}
		},
		methods: {
			goLogin(){
				uni.navigateTo({
					url: "/pages/login/login"
				})
			},
			//选择图片
			selectImg(){
				uni.chooseImage({
					count: 1, //默认9
					success: (res) => {
						//上传图片
						const tempFilePaths = res.tempFilePaths;
						uni.uploadFile({
							url: this.$.baseUrl + '/file/uploader', //仅为示例，非真实的接口地址
							filePath: tempFilePaths[0],
							name: 'file',
							success: (uploadFileRes) => {
								//JSON.parse 表示将JSON字符串转换成JSON对象
								var r = JSON.parse(uploadFileRes.data);
								console.log(r);
								//获取上传后的文件名称
								this.form.header = r.data;
								
								//头像回显
								this.urls = this.$.getImg(this.form.header);
							}
						});
					}
				});
			},
			//去注册
			register(){
				this.$.ajax({
					type: "POST",
					url: "/user/register",
					data: this.form,
					success: (result) => {
						uni.showToast({
							title: "注册成功！",
							icon:"success"
						})
						
						//清空form表单
						this.form = {
							username: "",
							password: "",
							nickname: "",
							email: "",
							header: ""
						}
						this.urls = "/static/header.jpeg";
					}
				});
			}
		}
	}
</script>

<style>
	
	.main_view {
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		align-items: center;
		height: 100%;
	}
	
	.top_view {
		margin-top: 150rpx;
		width: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		
	}
	
	.top_view > * {
		margin-bottom: 50rpx;
	}
	
	.header_cls {
		width: 200rpx;
		height: 200rpx;
		border-radius: 100rpx;
		box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
	}
	.input_cls {
		display: flex;
		justify-content: center;
		align-items: center;
	}
	
	.input_cls > input {
		margin-left: 20rpx;
		padding: 20rpx 30rpx;
		border-radius: 20rpx;
		box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
	}
	
	.btn_cls {
		width: 70%;
		background-color: #19b18b;
		color: white;
		margin-top: 40rpx;
		box-shadow: 10rpx 10rpx 10rpx gray;
	}
	
	.btn_login_cls {
		width: 70%;
		background-color: #525252;
		color: white;
		box-shadow: 10rpx 10rpx 10rpx gray;
	}
	
	button:active{
		position: relative;
		top: 5rpx;
		left: 5rpx;
		box-shadow: 5rpx 5rpx 10rpx gray;
	}
	
	.bottom_view {
		width: 100%;
		padding: 30rpx;
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;
	}
	
	.bottom_view {
		font-weight: 900;
		color: gray;
	}
</style>
